from .base_parser import BaseParser
import json

class GR45Parser(BaseParser):
    def parse(self, raw_data: bytes) -> Dict[str, Any]:
        try:
            data_dict = json.loads(raw_data.decode("utf-8"))
        except json.JSONDecodeError:
            raise ValueError("GR45数据不是有效的JSON格式")
        return {
            "pressure": data_dict.get("pressure"),
            "flow": data_dict.get("flow"),
            "device_id": data_dict.get("device_id"),
            "protocol": "GR45"
        }
    
    def get_metadata(self) -> Dict[str, str]:
        return {
            "format_name": "gr45_flowmeter_v1",
            "protocol": "GR45",
            "version": "1.0",
            "description": "支持GR45网口协议流量计数据解析"
        }